package com.team.ai.service;

import com.team.ai.entity.Question;
import com.team.ai.entity.ResultEntity;
import com.team.ai.entity.Score;
import com.team.ai.mapper.QuestionMapper;
import com.team.ai.mapper.ScoreMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class QuestionService {
    @Resource
    QuestionMapper questionMapper;
    @Resource
    ScoreMapper scoreMapper;
    public List<Question> getAllQuestions() {
        return questionMapper.getAllQuestions();
    }

    /**
     * 上传分数
     * @param userId
     * @param score
     * @return
     */
    public String uploadScore(int userId,int score){
        try{
            Score score1 = scoreMapper.selectById(userId);
            if (score1 == null){
                return ResultEntity.success(scoreMapper.insert(userId, score));
            }else{
                return ResultEntity.success(scoreMapper.update(userId, score+score1.getScore()));
            }
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }
    public String updateScore(int userId,int purchaseScore){
        try {
            Score score = scoreMapper.selectById(userId);
            if (score.getScore()<purchaseScore){
                return ResultEntity.success("积分不够");
            }else{
                return ResultEntity.success(scoreMapper.update(userId, score.getScore()-purchaseScore));
            }

        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }

    public String selectById(int userId){
        try {
                return ResultEntity.success(scoreMapper.selectById(userId));
        }catch (Exception e){
            e.printStackTrace();
            return  ResultEntity.fail(-10005);
        }
    }



}
